package com.ruoyi.web.controller.community;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.community.domain.HeartChildrenInfo;
import com.ruoyi.community.service.IHeartChildrenInfoService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 儿童信息Controller
 * 
 * @author Gaotian
 * @date 2021-12-16
 */
@RestController
@RequestMapping("/community/childinfo")
public class HeartChildrenInfoController extends BaseController
{
    @Autowired
    private IHeartChildrenInfoService heartChildrenInfoService;

    /**
     * 查询儿童信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(HeartChildrenInfo heartChildrenInfo)
    {
        startPage();
        List<HeartChildrenInfo> list = heartChildrenInfoService.selectHeartChildrenInfoList(heartChildrenInfo);
        return getDataTable(list);
    }

    @GetMapping("/byUserId")
    public TableDataInfo byUserId()
    {
        startPage();
        Long userid = SecurityUtils.getUserID();
        HeartChildrenInfo heartChildrenInfo = new HeartChildrenInfo();
        heartChildrenInfo.setUserId(userid);
        List<HeartChildrenInfo> list = heartChildrenInfoService.selectHeartChildrenInfoList(heartChildrenInfo);
        return getDataTable(list);
    }

    /**
     * 导出儿童信息列表
     */
    @Log(title = "儿童信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HeartChildrenInfo heartChildrenInfo)
    {
        List<HeartChildrenInfo> list = heartChildrenInfoService.selectHeartChildrenInfoList(heartChildrenInfo);
        ExcelUtil<HeartChildrenInfo> util = new ExcelUtil<HeartChildrenInfo>(HeartChildrenInfo.class);
        return util.exportExcel(list, "儿童信息数据");
    }

    /**
     * 获取儿童信息详细信息
     */
    @GetMapping(value = "/{childid}")
    public AjaxResult getInfo(@PathVariable("childid") Long childid)
    {
        return AjaxResult.success(heartChildrenInfoService.selectHeartChildrenInfoById(childid));
    }

    /**
     * 新增儿童信息
     */
    @Log(title = "儿童信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HeartChildrenInfo heartChildrenInfo)
    {
        Long userid = SecurityUtils.getUserID();
        HeartChildrenInfo temp = new HeartChildrenInfo();
        temp.setUserId(userid);
        List<HeartChildrenInfo> list = heartChildrenInfoService.selectHeartChildrenInfoList(temp);
        if(list.size() > 0){
            return AjaxResult.error(300,"您已认证过");
        }
        return toAjax(heartChildrenInfoService.insertHeartChildrenInfo(heartChildrenInfo));
    }

    /**
     * 修改儿童信息
     */
    @Log(title = "儿童信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HeartChildrenInfo heartChildrenInfo)
    {
        return toAjax(heartChildrenInfoService.updateHeartChildrenInfo(heartChildrenInfo));
    }

    /**
     * 删除儿童信息
     */
    @Log(title = "儿童信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{childids}")
    public AjaxResult remove(@PathVariable Long[] childids)
    {
        return toAjax(heartChildrenInfoService.deleteHeartChildrenInfoByIds(childids));
    }
}
